/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

// TODO sparwidzic lenght (TL) w ip


/*
 * MainWindow.java
 *
 * Created on 2010-01-06, 13:48:26
 */

package efe.gui;

import efe.JpcapTools;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.UnknownHostException;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;
import jpcap.*;
import jpcap.packet.*;

/**
 * Główne okno programu.
 * @author Paweł
 */
public class MainWindow extends javax.swing.JFrame implements Serializable {

    private JpcapSender sendingInterface;
    private JFileChooser jFileChooser;

    /** Creates new form MainWindow */
    public MainWindow() {
        add(new TCPPanel());
        initComponents();
        initSendingInterfaceList();
        initFileChooser();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        IPTCPDialog = new javax.swing.JDialog();
        IPPanel4TCPPanel = new efe.gui.IPPanel();
        TCPPanel = new efe.gui.TCPPanel();
        jButton2 = new javax.swing.JButton();
        IPTCPData = new efe.gui.DataPanel();
        dstMAC = new efe.bean.EfeMACValue();
        srcMAC = new efe.bean.EfeMACValue();
        type = new efe.bean.EfeNumberValue();
        IPUDPDialog = new javax.swing.JDialog();
        IPPanel4UDPPanel = new efe.gui.IPPanel();
        UDPPanel = new efe.gui.UDPPanel();
        IPUDPData = new efe.gui.DataPanel();
        jButton3 = new javax.swing.JButton();
        ICMPDialog = new javax.swing.JDialog();
        IPPanel4ICMPPanel = new efe.gui.IPPanel();
        ICMPPanel = new efe.gui.ICMPPanel();
        ICMPData = new efe.gui.DataPanel();
        jButton4 = new javax.swing.JButton();
        ARPDialog = new javax.swing.JDialog();
        aRPPanel1 = new efe.gui.ARPPanel();
        jButton5 = new javax.swing.JButton();
        AboutBox = new javax.swing.JDialog();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jButton6 = new javax.swing.JButton();
        jRadioButton1 = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        IPRadioButton = new javax.swing.JRadioButton();
        ARPRadioButton = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();
        TCPRadioButton = new javax.swing.JRadioButton();
        UDPRadioButton = new javax.swing.JRadioButton();
        ICMPRadioButton = new javax.swing.JRadioButton();
        jButton1 = new javax.swing.JButton();
        dataExpandField = new efe.bean.EfeEditorExpandField();
        typeEditorField = new efe.bean.EfeEditorField();
        efeEditorField2 = new efe.bean.EfeEditorField();
        efeEditorField1 = new efe.bean.EfeEditorField();
        efeEditorBlankField1 = new efe.bean.EfeEditorBlankField();
        jComboBox1 = new javax.swing.JComboBox();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        openFileMenu = new javax.swing.JMenuItem();
        saveFileMenu = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        IPTCPDialog.setTitle("IP + TCP headers + DATA");
        IPTCPDialog.setForeground(java.awt.Color.white);
        IPTCPDialog.setMinimumSize(new java.awt.Dimension(971, 650));
        IPTCPDialog.setLocationRelativeTo(null);

        jButton2.setText("OK");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout IPTCPDialogLayout = new javax.swing.GroupLayout(IPTCPDialog.getContentPane());
        IPTCPDialog.getContentPane().setLayout(IPTCPDialogLayout);
        IPTCPDialogLayout.setHorizontalGroup(
            IPTCPDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(IPTCPData, javax.swing.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE)
            .addComponent(IPPanel4TCPPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(TCPPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE)
        );
        IPTCPDialogLayout.setVerticalGroup(
            IPTCPDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(IPTCPDialogLayout.createSequentialGroup()
                .addComponent(IPPanel4TCPPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TCPPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(IPTCPData, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2))
        );

        IPTCPDialogLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {IPPanel4TCPPanel, TCPPanel});

        type.setLimit(3278);
        type.validate(String.valueOf(EthernetPacket.ETHERTYPE_IP));

        IPUDPDialog.setTitle("IP + UDP headers + Data");
        IPUDPDialog.setMinimumSize(new java.awt.Dimension(971, 400));
        IPUDPDialog.setLocationRelativeTo(null);

        jButton3.setText("OK");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout IPUDPDialogLayout = new javax.swing.GroupLayout(IPUDPDialog.getContentPane());
        IPUDPDialog.getContentPane().setLayout(IPUDPDialogLayout);
        IPUDPDialogLayout.setHorizontalGroup(
            IPUDPDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(UDPPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE)
            .addComponent(IPUDPData, javax.swing.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE)
            .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE)
            .addComponent(IPPanel4UDPPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE)
        );
        IPUDPDialogLayout.setVerticalGroup(
            IPUDPDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(IPUDPDialogLayout.createSequentialGroup()
                .addComponent(IPPanel4UDPPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(UDPPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 54, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(IPUDPData, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3))
        );

        ICMPDialog.setTitle("ICMP header + Data");
        ICMPDialog.setMinimumSize(new java.awt.Dimension(970, 350));
        ICMPDialog.setLocationRelativeTo(null);

        jButton4.setText("OK");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout ICMPDialogLayout = new javax.swing.GroupLayout(ICMPDialog.getContentPane());
        ICMPDialog.getContentPane().setLayout(ICMPDialogLayout);
        ICMPDialogLayout.setHorizontalGroup(
            ICMPDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(ICMPData, 0, 0, Short.MAX_VALUE)
            .addComponent(ICMPPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE)
            .addComponent(IPPanel4ICMPPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE)
        );
        ICMPDialogLayout.setVerticalGroup(
            ICMPDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ICMPDialogLayout.createSequentialGroup()
                .addComponent(IPPanel4ICMPPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ICMPPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 54, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ICMPData, javax.swing.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4))
        );

        ARPDialog.setTitle("ARP header");
        ARPDialog.setAlwaysOnTop(true);
        ARPDialog.setMinimumSize(new java.awt.Dimension(970, 264));
        ARPDialog.setResizable(false);
        ARPDialog.setLocationRelativeTo(null);

        aRPPanel1.setMinimumSize(new java.awt.Dimension(970, 189));

        jButton5.setText("OK");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout ARPDialogLayout = new javax.swing.GroupLayout(ARPDialog.getContentPane());
        ARPDialog.getContentPane().setLayout(ARPDialogLayout);
        ARPDialogLayout.setHorizontalGroup(
            ARPDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, 970, Short.MAX_VALUE)
            .addComponent(aRPPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 970, Short.MAX_VALUE)
        );
        ARPDialogLayout.setVerticalGroup(
            ARPDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ARPDialogLayout.createSequentialGroup()
                .addComponent(aRPPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton5))
        );

        AboutBox.setTitle("O Programie");
        AboutBox.setForeground(java.awt.Color.white);
        AboutBox.setMinimumSize(new java.awt.Dimension(255, 230));
        AboutBox.setLocationRelativeTo(null);

        jLabel5.setText("EthFrameEditor  v. 0.1");

        jLabel6.setText("Program do edycji ramek ethernet");

        jLabel7.setText("Autorzy:");

        jLabel8.setText("Paweł Kopiczko");

        jLabel9.setText("Paweł Konopko");

        jButton6.setText("Zamknij");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout AboutBoxLayout = new javax.swing.GroupLayout(AboutBox.getContentPane());
        AboutBox.getContentPane().setLayout(AboutBoxLayout);
        AboutBoxLayout.setHorizontalGroup(
            AboutBoxLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AboutBoxLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(AboutBoxLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(AboutBoxLayout.createSequentialGroup()
                        .addGroup(AboutBoxLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton6, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addContainerGap())
                    .addGroup(AboutBoxLayout.createSequentialGroup()
                        .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(95, 95, 95))
                    .addGroup(AboutBoxLayout.createSequentialGroup()
                        .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
                        .addGap(30, 30, 30)
                        .addGroup(AboutBoxLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(AboutBoxLayout.createSequentialGroup()
                                .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(1, 1, 1)))
                        .addGap(52, 52, 52))))
        );
        AboutBoxLayout.setVerticalGroup(
            AboutBoxLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AboutBoxLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addGap(31, 31, 31)
                .addComponent(jLabel6)
                .addGap(34, 34, 34)
                .addGroup(AboutBoxLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel9)
                .addGap(18, 18, 18)
                .addComponent(jButton6)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Edytor ramek ethernet");

        jRadioButton1.setSelected(true);
        jRadioButton1.setText("Ethernet");
        jRadioButton1.setEnabled(false);

        jLabel1.setText("Warstwy:");

        jLabel2.setText("Łącza danych");

        jLabel3.setText("Sieciowa");

        IPRadioButton.setSelected(true);
        IPRadioButton.setText("IP");
        IPRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                IPRadioButtonActionPerformed(evt);
            }
        });

        ARPRadioButton.setText("ARP");
        ARPRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ARPRadioButtonActionPerformed(evt);
            }
        });

        jLabel4.setText("Transportowa");

        TCPRadioButton.setSelected(true);
        TCPRadioButton.setText("TCP");
        TCPRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TCPRadioButtonActionPerformed(evt);
            }
        });

        UDPRadioButton.setText("UDP");
        UDPRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                UDPRadioButtonActionPerformed(evt);
            }
        });

        ICMPRadioButton.setText("ICMP");
        ICMPRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ICMPRadioButtonActionPerformed(evt);
            }
        });

        jButton1.setText("Wyślij");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        dataExpandField.setText("Data");
        dataExpandField.setFont(new java.awt.Font("DejaVu Sans", 0, 12));
        dataExpandField.setjDialog(IPTCPDialog);

        typeEditorField.setFont(new java.awt.Font("DejaVu Sans", 0, 12));
        typeEditorField.setLabelText("Type");
        typeEditorField.setValue(type);

        efeEditorField2.setAlignmentX(0.0F);
        efeEditorField2.setAlignmentY(0.0F);
        efeEditorField2.setFont(new java.awt.Font("DejaVu Sans", 0, 12));
        efeEditorField2.setLabelText("Src MAC");
        efeEditorField2.setValue(srcMAC);

        efeEditorField1.setAlignmentX(0.0F);
        efeEditorField1.setAlignmentY(0.0F);
        efeEditorField1.setFont(new java.awt.Font("DejaVu Sans", 0, 12)); // NOI18N
        efeEditorField1.setLabelText("Dst MAC");
        efeEditorField1.setValue(dstMAC);

        efeEditorBlankField1.setText("CRC");
        efeEditorBlankField1.setFont(new java.awt.Font("DejaVu Sans", 0, 12));

        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jMenu1.setText("Plik");

        openFileMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        openFileMenu.setText("Otwórz...");
        openFileMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openFileMenuActionPerformed(evt);
            }
        });
        jMenu1.add(openFileMenu);

        saveFileMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        saveFileMenu.setText("Zapisz...");
        saveFileMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveFileMenuActionPerformed(evt);
            }
        });
        jMenu1.add(saveFileMenu);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("O Programie");

        jMenuItem1.setText("Autorzy");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addComponent(efeEditorField1, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                .addGap(0, 0, 0)
                .addComponent(efeEditorField2, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                .addGap(0, 0, 0)
                .addComponent(typeEditorField, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
                .addGap(0, 0, 0)
                .addComponent(dataExpandField, javax.swing.GroupLayout.DEFAULT_SIZE, 290, Short.MAX_VALUE)
                .addGap(0, 0, 0)
                .addComponent(efeEditorBlankField1, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(58, 58, 58)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jRadioButton1))
                .addGap(95, 95, 95)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(IPRadioButton)
                    .addComponent(jLabel3))
                .addGap(100, 100, 100)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(TCPRadioButton)
                        .addGap(169, 169, 169)
                        .addComponent(ARPRadioButton)
                        .addGap(135, 135, 135)
                        .addComponent(ICMPRadioButton))
                    .addComponent(jLabel4)
                    .addComponent(UDPRadioButton))
                .addContainerGap(41, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jComboBox1, 0, 798, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jLabel3))
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButton1)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(TCPRadioButton)
                                .addComponent(ARPRadioButton)
                                .addComponent(ICMPRadioButton)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(UDPRadioButton))
                    .addComponent(IPRadioButton))
                .addGap(64, 64, 64)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(efeEditorField1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
                    .addComponent(efeEditorBlankField1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
                    .addComponent(dataExpandField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
                    .addComponent(typeEditorField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
                    .addComponent(efeEditorField2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-995)/2, (screenSize.height-387)/2, 995, 387);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Packet p = getCurrentPacket();
        sendingInterface.sendPacket(p);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void IPRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_IPRadioButtonActionPerformed
        //if (!IPRadioButton.isSelected()) { return; }
        if (IPRadioButton.isSelected()) {
            setEthTypeToIP();
        }
        IPRadioButton.setSelected(true);
        TCPRadioButton.setSelected(true);
        UDPRadioButton.setSelected(false);
        ICMPRadioButton.setSelected(false);
        ARPRadioButton.setSelected(false);
        
    }//GEN-LAST:event_IPRadioButtonActionPerformed

    private void ARPRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ARPRadioButtonActionPerformed
        if (ARPRadioButton.isSelected()) {
            type.setNumber(EthernetPacket.ETHERTYPE_ARP);
            typeEditorField.update();
            dataExpandField.setjDialog(ARPDialog);
        }
        IPRadioButton.setSelected(false);
        TCPRadioButton.setSelected(false);
        UDPRadioButton.setSelected(false);
        ICMPRadioButton.setSelected(false);
        ARPRadioButton.setSelected(true);
    }//GEN-LAST:event_ARPRadioButtonActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        IPTCPDialog.setVisible(false);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void TCPRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TCPRadioButtonActionPerformed
        if (TCPRadioButton.isSelected()) {
            setEthTypeToIP();
            dataExpandField.setjDialog(IPTCPDialog);
        }
        IPRadioButton.setSelected(true);
        TCPRadioButton.setSelected(true);
        UDPRadioButton.setSelected(false);
        ICMPRadioButton.setSelected(false);
        ARPRadioButton.setSelected(false);
    }//GEN-LAST:event_TCPRadioButtonActionPerformed

    private void UDPRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_UDPRadioButtonActionPerformed
        if (UDPRadioButton.isSelected()) {
            setEthTypeToIP();
            dataExpandField.setjDialog(IPUDPDialog);
        }
        IPRadioButton.setSelected(true);
        TCPRadioButton.setSelected(false);
        UDPRadioButton.setSelected(true);
        ICMPRadioButton.setSelected(false);
        ARPRadioButton.setSelected(false);
    }//GEN-LAST:event_UDPRadioButtonActionPerformed

    private void ICMPRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ICMPRadioButtonActionPerformed
        if (ICMPRadioButton.isSelected()) {
            setEthTypeToIP();
            dataExpandField.setjDialog(ICMPDialog);
        }
        IPRadioButton.setSelected(true);
        TCPRadioButton.setSelected(false);
        UDPRadioButton.setSelected(false);
        ICMPRadioButton.setSelected(true);
        ARPRadioButton.setSelected(false);
    }//GEN-LAST:event_ICMPRadioButtonActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        IPUDPDialog.setVisible(false);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        ICMPDialog.setVisible(false);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void openFileMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openFileMenuActionPerformed
        int returnVal = jFileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = jFileChooser.getSelectedFile();
                try {
                    Object o = new ObjectInputStream(new FileInputStream(file)).readObject();
                    if (o instanceof Vector) {
                        Vector<Object> v = (Vector<Object>) o;
                        Iterator<Object> i = ((Vector<Object>) o).iterator();
                        String ext = (String) i.next();
                        if (ext.equals(JpcapTools.TCPExtension)) {
                            IPPanel4TCPPanel.setValuesFromVector((Vector<Object>) i.next());
                            TCPPanel.setValuesFromVector((Vector<Object>) i.next());
                            IPTCPData.setDataString((String) i.next());
                        }
                        if (ext.equals(JpcapTools.UDPExtension)) {
                            IPPanel4UDPPanel.setValuesFromVector((Vector<Object>) i.next());
                            UDPPanel.setValuesFromVector((Vector<Object>) i.next());
                            IPUDPData.setDataString((String) i.next());
                        }
                        if (ext.equals(JpcapTools.ICMPExtension)) {
                            IPPanel4ICMPPanel.setValuesFromVector((Vector<Object>) i.next());
                            ICMPPanel.setValuesFromVector((Vector<Object>) i.next());
                            ICMPData.setDataString((String) i.next());
                        }
                        if (ext.equals(JpcapTools.ARPExtension)) {
                            aRPPanel1.setValuesFromVector((Vector<Object>) i.next());
                        }
                    }
                } catch (IOException ex) {
                    Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
    }//GEN-LAST:event_openFileMenuActionPerformed

    private void saveFileMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveFileMenuActionPerformed
        int returnVal = jFileChooser.showSaveDialog(this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = jFileChooser.getSelectedFile();
                Vector<Object> v = new Vector<Object>();
                if (ARPRadioButton.isSelected()) {
                    file = new File(file.toString() + "." + JpcapTools.ARPExtension);
                    v.add(JpcapTools.ARPExtension);
                    v.add(aRPPanel1.getValuesVector());
                } else if (TCPRadioButton.isSelected()) {
                    file = new File(file.toString() + "." + JpcapTools.TCPExtension);
                    v.add(JpcapTools.TCPExtension);
                    v.add(IPPanel4TCPPanel.getValuesVector());
                    v.add(TCPPanel.getValuesVector());
                    v.add(IPTCPData.getDataString());
                } else if (UDPRadioButton.isSelected()) {
                    file = new File(file.toString() + "." + JpcapTools.UDPExtension);
                    v.add(JpcapTools.UDPExtension);
                    v.add(IPPanel4UDPPanel.getValuesVector());
                    v.add(UDPPanel.getValuesVector());
                    v.add(IPUDPData.getDataString());
                } else if (ICMPRadioButton.isSelected()) {
                    file = new File(file.toString() + "." + JpcapTools.ICMPExtension);
                    v.add(JpcapTools.ICMPExtension);
                    v.add(IPPanel4ICMPPanel.getValuesVector());
                    v.add(ICMPPanel.getValuesVector());
                    v.add(ICMPData.getDataString());
                }
                try {
                    new ObjectOutputStream(new FileOutputStream(file))
                            .writeObject(v);
                } catch (IOException ex) {
                    Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);

                }
                System.out.println(file);
            }
    }//GEN-LAST:event_saveFileMenuActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        try {
            sendingInterface = JpcapTools.getSendingDevice(jComboBox1.getSelectedIndex());
        } catch (IOException ex) {
        }
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        ARPDialog.setVisible(false);
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        AboutBox.setVisible(false);
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        AboutBox.setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    /**
     * Tworzy główne okno.
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //Turn off metal's use of bold fonts
                UIManager.put("swing.boldMetal", Boolean.FALSE);
                new MainWindow().setVisible(true);
            }
        });
    }

    private Packet getCurrentPacket() {
        try {
            EthernetPacket ether = new EthernetPacket();
            Packet packet;
            ether.dst_mac = (byte[]) dstMAC.getValue();
            ether.src_mac = (byte[]) srcMAC.getValue();
            ether.frametype = (short) type.getValue();
            // jeśli aktualny pakiet to TCP
            if (TCPRadioButton.isSelected()) {
                // tego juz nie robie bo jest mozliwosc edycji dla usera
                //ether.frametype = EthernetPacket.ETHERTYPE_IP;
                jpcap.packet.TCPPacket tcpPacket = TCPPanel.getTCPPacket();
                tcpPacket = (TCPPacket) IPPanel4TCPPanel.setIPHeader(tcpPacket,
                        TCPPacket.IPPROTO_TCP);
                tcpPacket.data = IPTCPData.getData(); // dane pakietu TCP
                tcpPacket.datalink = ether; // ustawineie wartstwy lacza danych
                return tcpPacket;
            }
            // jesli aktualnyu pakiet to UDP
            if (UDPRadioButton.isSelected()) {
                UDPPacket udpPacket = UDPPanel.getUDPPacket();
                udpPacket = (UDPPacket) IPPanel4UDPPanel.setIPHeader(udpPacket,
                        UDPPacket.IPPROTO_UDP);
                udpPacket.data = IPUDPData.getData();
                udpPacket.datalink = ether;
                return udpPacket;
            }
            if (ICMPRadioButton.isSelected()) {
                ICMPPacket p = ICMPPanel.getICMPPacket();
                p = (ICMPPacket) IPPanel4ICMPPanel.setIPHeader(p,
                        ICMPPacket.IPPROTO_ICMP);
                p.data = ICMPData.getData();
                p.datalink = ether;
                return p;
            }
            if (ARPRadioButton.isSelected()) {
                ARPPacket p = aRPPanel1.getARPPacket();
                p.datalink = ether;
                return p;
            }

        } catch (UnknownHostException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
        JOptionPane.showMessageDialog(null, "Ta wiadomość nie powinna się" +
                " pojawić, jeśli ją zobaczyłeś to powiadmom autorów i opisz" +
                " co dokładnie robiłeś.");
        throw new UnsupportedOperationException();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDialog ARPDialog;
    private javax.swing.JRadioButton ARPRadioButton;
    private javax.swing.JDialog AboutBox;
    private efe.gui.DataPanel ICMPData;
    private javax.swing.JDialog ICMPDialog;
    private efe.gui.ICMPPanel ICMPPanel;
    private javax.swing.JRadioButton ICMPRadioButton;
    private efe.gui.IPPanel IPPanel4ICMPPanel;
    private efe.gui.IPPanel IPPanel4TCPPanel;
    private efe.gui.IPPanel IPPanel4UDPPanel;
    private javax.swing.JRadioButton IPRadioButton;
    private efe.gui.DataPanel IPTCPData;
    private javax.swing.JDialog IPTCPDialog;
    private efe.gui.DataPanel IPUDPData;
    private javax.swing.JDialog IPUDPDialog;
    private efe.gui.TCPPanel TCPPanel;
    private javax.swing.JRadioButton TCPRadioButton;
    private efe.gui.UDPPanel UDPPanel;
    private javax.swing.JRadioButton UDPRadioButton;
    private efe.gui.ARPPanel aRPPanel1;
    private efe.bean.EfeEditorExpandField dataExpandField;
    private efe.bean.EfeMACValue dstMAC;
    private efe.bean.EfeEditorBlankField efeEditorBlankField1;
    private efe.bean.EfeEditorField efeEditorField1;
    private efe.bean.EfeEditorField efeEditorField2;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JMenuItem openFileMenu;
    private javax.swing.JMenuItem saveFileMenu;
    private efe.bean.EfeMACValue srcMAC;
    private efe.bean.EfeNumberValue type;
    private efe.bean.EfeEditorField typeEditorField;
    // End of variables declaration//GEN-END:variables

    private void initSendingInterfaceList() {
        try {
            for (String dev : JpcapTools.getDevList()) {
                jComboBox1.addItem(dev);
            }
            sendingInterface = JpcapTools.getSendingDevice(0);
        } catch (IOException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void setEthTypeToIP() {
        type.setNumber(EthernetPacket.ETHERTYPE_IP);
        typeEditorField.update();
    }

    private void initFileChooser() {
        jFileChooser  = new JFileChooser();
        jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        jFileChooser.removeChoosableFileFilter(
                jFileChooser.getChoosableFileFilters()[0]);
        jFileChooser.addChoosableFileFilter(new FileFilter() {
            @Override
            public boolean accept(File file) {
                if (file.isDirectory()) return true;
                String ext = JpcapTools.getExtension(file);
                if (ext.equals(JpcapTools.TCPExtension)
                        || ext.equals(JpcapTools.UDPExtension)
                        || ext.equals(JpcapTools.ICMPExtension)
                        || ext.equals(JpcapTools.ARPExtension)) {
                    return true;
                }
                return false;
            }
            @Override
            public String getDescription() {
                return "Pakiety programu EthFrameEdit";
            }
        });
    }

}
